// pages/addPublish/addPublish.js
const call = require('../../utils/request.js')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    array: ['餐厅', '图书馆', '约拍', '快递', '活动', '其他'],
    sortIndex: null,
    contentCount: 0,
    allImages:[],
    jobImgList: [],
    uploading: false
  },

  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      sortIndex: e.detail.value
    })
  },

  handleContentInput(e) {
    const value = e.detail.value
    this.setData({
      contentCount: value.length 
    })
  },

  chooseImg() {
    const that = this;
    const allImages = this.data.allImages;
    const jobImgList = this.data.jobImgList
    wx.chooseImage({
      count: 5 - allImages.length,
      success(res) {
        allImages.push(...res.tempFilePaths)
        const myImages = res.tempFilePaths
        for (let i in myImages) {
          that.setData({ uploading: true })
          call.requestFile('pub/uploadImg', myImages[i],
            function (e) {
              jobImgList.push(e)
              that.setData({ uploading: false })
            }, function () {
              that.setData({ uploading: false })
              wx.showToast({
                title: '上传图片失败',
                icon: 'none',
                duration: 2000
              })
              return
            })
        }
        that.setData({
          allImages: allImages,
          jobImgList: jobImgList
        })
      }
    })
  },

  handleImagePreview(e) {
    console.log(e)
    const idx = e.target.dataset.idx
    const myImages = this.data.allImages
    wx.previewImage({
      current: myImages[idx], // 当前显示图片的http链接
      urls: myImages // 需要预览的图片http链接列表
    })
  },

  removeImage(e) {
    const idx = e.target.dataset.idx
    const allImages = this.data.allImages
    const jobImgList = this.data.jobImgList
    allImages.splice(idx, 1);
    jobImgList.splice(idx, 1);
    this.setData({
      allImages: allImages,
      jobImgList: jobImgList
    })
  },

  addPublish(e) {
    console.log(e);
    console.log(this.data.allImages)
    if (!this.data.allImages.length){
      wx.showToast({
        title: '请上传至少一张图片',
        icon: 'none',
        duration: 2000
      })
      return
    } else if (this.data.loading) {
      wx.showToast({
        title: '还有图片正在上传',
        icon: 'none',
        duration: 2000
      })
    }
    const formMsg = e.detail.value
    for (let key in formMsg) {
      if(!formMsg[key]){
        wx.showToast({
          title: '请将信息填写完整',
          icon: 'none',
          duration: 2000
        })
      return
      }
    }
    if (!(/^1[3456789]\d{9}$/.test(formMsg.tel))) {
      wx.showToast({
        title: '手机号码有误，请重填',
        icon: 'none',
        duration: 2000
      })
      return
    }
    wx.showLoading({
      title: '职位创建中',
    })
    const openId = wx.getStorageSync('token')
    const id = this.data.id
    const sort = this.data.sortIndex
    const jobImgList = this.data.jobImgList
    console.log('aaaa', jobImgList)
    const that = this
    call.requestPost('pub/insertJob', {
      openId: openId,
      id: id,
      name: formMsg.name,
      price: formMsg.price,
      recruitnum: formMsg.recruitnum,
      instru: formMsg.instru,
      detail: formMsg.detail,
      contact: formMsg.contact,
      tel: formMsg.tel,
      adress: formMsg.adress,
      sort: sort,
      jobImgList: jobImgList
    }, function (e) {
      console.log('addPublish', e)
      wx.hideLoading()
      wx.showToast({
        title: '发布成功',
        icon: 'success',
        duration: 2000,
        success: function () {
          setTimeout(function () {
            wx.navigateBack({
              delta: 1
            })
          }, 2000)
        }
      })
    }, function () {
      wx.hideLoading()
      wx.showToast({
        title: '前方拥挤，创建失败',
        icon: null,
        duration: 2000
      })
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      id: options.id
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    const id = this.data.id
    if(id){
      const openId = wx.getStorageSync('token') || '';
      const that = this
      call.requestGet('jus/findOne', {
        openId: openId,
        id: id
      }, function (e) {
        console.log('jobDetail', e)
        that.setData({
          name: e.name,
          price: e.price,
          recruitnum: e.recruitnum,
          instru: e.instru,
          detail: e.detail,
          contact: e.contact,
          tel: e.tel,
          sortIndex: e.sort,
          adress: e.adress
        })
      }, function () {

      });
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})